package com.allenjsl.springactuator.controller;

import com.alibaba.fastjson.JSON;
import com.allenjsl.springactuator.entity.User;
import com.allenjsl.springactuator.service.ProducerService;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

@RequestMapping("/producer")
@RestController
public class ProducerController {

    @Autowired
    private ProducerService producerService;
    // 发送用户消费消息
    @GetMapping("/charge")
    public Map<String,Object> charge() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        User user = new User();
        Integer money = new Random().nextInt(100);
        user.setName("allenjsl" + money);
        user.setId(UUID.randomUUID().toString());
        user.setBalance(money);
        Map<String,Object> result = new HashMap<String,Object>();
        producerService.sendHalfMsg(user);
//        TransactionSendResult sendResult = producerService.sendHalfMsg(user);

        result.put("code","0");
        result.put("msg","发送成功");
//        result.put("data",sendResult);
        result.put("user", JSON.toJSONString(user));
        return result;
    }
}

